//
//  AddFriendsViewController.m
//  EasyChat
//
//  Created by romana on 16/6/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AddFriendsViewController.h"
#import "AddFriendTableViewCell.h"

@interface AddFriendsViewController ()<UITextFieldDelegate,EMChatManagerDelegate>


//@property (strong, nonatomic) UITextField *textField;
@property (retain, nonatomic) NSIndexPath *selectedIndexPath;

@end

@implementation AddFriendsViewController







- (void)dealloc
{
    [super dealloc];
    [_myTableView release];
    [_dataSource release];
    [_textField release];
    [_headerView release];
    [_selectedIndexPath release];
    
 //   [[EaseMob sharedInstance].chatManager removeDelegate:self];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"添加好友";
    //左按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(leftBarAction)];
    //右按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(rightBarAction)];
    //设置按钮颜色
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    //修改导航栏标题的颜色 和 大小
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],

                                                                      NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //设置textfield
            _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, 40)];
            _textField.layer.borderColor = [[UIColor lightGrayColor] CGColor];
            _textField.layer.borderWidth = 0.5;
            _textField.layer.cornerRadius = 3;
            _textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];
            _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            _textField.leftViewMode = UITextFieldViewModeAlways;
            _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            _textField.font = [UIFont systemFontOfSize:15.0];
            _textField.backgroundColor = [UIColor whiteColor];
            _textField.placeholder = @"输入要查找的好友";
            _textField.returnKeyType = UIReturnKeyDone;
            _textField.delegate = self;
    //监听textfield的输入内容  每当内容改变 都走方法
            [self.textField addTarget:self  action:@selector(valueChanged)  forControlEvents:UIControlEventEditingChanged];
    
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 60)];
            self.headerView.backgroundColor = [UIColor colorWithRed:0.88 green:0.88 blue:0.88 alpha:1.0];
    
            [self.headerView addSubview:self.textField];
    [self.view addSubview:self.headerView];
    
    self.Aview = [[UIView alloc] initWithFrame:CGRectMake(0, 124, self.view.frame.size.width, 50)];
    self.Aview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.Aview];
    self.Aview.hidden = YES;
    [_Aview release];
    
    self.AImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 10, 30, 29)];
    self.AImageView.backgroundColor = [UIColor whiteColor];
    self.AImageView.image = [UIImage imageNamed:@"chatListCellHead.png"];
    [self.Aview addSubview:self.AImageView];
    [_AImageView release];
    
    self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 10, 200, 29)];
    self.textLabel.backgroundColor = [UIColor whiteColor];
    [self.Aview addSubview:self.textLabel];
    [_textLabel release];
    
    self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.myButton.frame = CGRectMake(self.view.frame.size.width - 130, 10, 70, 29);
    self.myButton.backgroundColor = [UIColor colorWithRed:10 / 255.0 green:82 / 255.0 blue:104 / 255.0 alpha:1.0];
    [self.myButton setTitle:@"添加" forState:UIControlStateNormal];
    [self.myButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.Aview addSubview:self.myButton];
    [_myButton release];
    
    UIView *gapView = [[UIView alloc] initWithFrame:CGRectMake(0, 49, self.view.frame.size.width, 1)];
    gapView.backgroundColor = [UIColor grayColor];
    [self.Aview addSubview:gapView];
    [gapView release];
    
    
    //添加(聊天管理器)代理
  //  [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    
    
    
    
    
}
-(void)valueChanged{

    self.textLabel.text = self.textField.text;


}
-(void)rightBarAction{

    self.Aview.hidden = NO;


}


-(void)leftBarAction{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}

//添加好友button 点击方法
-(void)buttonAction{

//1.获取要添加好友的名字
    NSString *username = self.textField.text;
 //2.向服务器发送一个添加好友的请求
    //message  请求添加好友的额外信息
 //当前登录名字
    NSString *loginUserName = [[EaseMob sharedInstance].chatManager loginInfo][@"username"];
    EMError *error = nil;
    
    NSString *userMessage = [@"我是" stringByAppendingString:loginUserName];
    [[EaseMob sharedInstance].chatManager addBuddy:username message:userMessage error:&error];

    if (error) {
        NSLog(@"添加好友有问题%@",error);
    }else{
    
        NSLog(@"添加好友没有问题");
    
    }


}




//    self.myTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
//    self.myTableView.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:self.myTableView];
//    
//    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.myTableView.tableHeaderView = self.headerView;
//    
//    self.myTableView.delegate = self;
//    self.myTableView.dataSource = self;
//    
//    [self.myTableView registerClass:[AddFriendTableViewCell class] forCellReuseIdentifier:@"reuse"];
//    
//    [_myTableView release];
//
//  //  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeText) name:UITextFieldTextDidBeginEditingNotification object:nil];
//    
//    
//    // Do any additional setup after loading the view.
//}
//

//
//#pragma mark - getter
//
//- (UITextField *)textField
//{
//    if (_textField == nil) {
//        _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, 40)];
//        _textField.layer.borderColor = [[UIColor lightGrayColor] CGColor];
//        _textField.layer.borderWidth = 0.5;
//        _textField.layer.cornerRadius = 3;
//        _textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];
//        _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//        _textField.leftViewMode = UITextFieldViewModeAlways;
//        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//        _textField.font = [UIFont systemFontOfSize:15.0];
//        _textField.backgroundColor = [UIColor whiteColor];
//        _textField.placeholder = @"输入要查找的好友";
//        _textField.returnKeyType = UIReturnKeyDone;
//        _textField.delegate = self;
//        [self.textField addTarget:self  action:@selector(valueChanged)  forControlEvents:UIControlEventEditingChanged];
//    }
//    
//    return _textField;
//}
//
//-(void)valueChanged{
//
//  //  NSLog(@"值改变了");
//    
////    AddFriendTableViewCell *AFCell = [AddFriendTableViewCell new];
////    AFCell.textLabel.text = self.textField.text;
//
//    NSLog(@"%@",self.textField.text);
//    
//}
//
//- (UIView *)headerView
//{
//    if (_headerView == nil) {
//        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.myTableView.frame.size.width, 60)];
//        _headerView.backgroundColor = [UIColor colorWithRed:0.88 green:0.88 blue:0.88 alpha:1.0];
//        
//        [_headerView addSubview:self.textField];
//    }
//    
//    return _headerView;
//}
//
//#pragma mark - Table view data source
//
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    // Return the number of sections.
//    return 1;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    // Return the number of rows in the section.
//    return 5;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
////    static NSString *CellIdentifier = @"AddFriendCell";
//    AddFriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
//   // cell.textLabel.text = @"123";
//    // Configure the cell...
//    if (cell == nil) {
//        cell = [[AddFriendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
//    }
////    
//    cell.imageView.image = [UIImage imageNamed:@"chatListCellHead.png"];
//  //  cell.textLabel.text = [self.dataSource objectAtIndex:indexPath.row];
//    cell.textLabel.text = self.textField.text;
//    return cell;
//}
//
//
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 50;
//
//}
//
//
//#pragma mark - UITextFieldDelegate 回收键盘
//
//- (BOOL)textFieldShouldReturn:(UITextField *)textField
//{
//    [textField resignFirstResponder];
//    return YES;
//}
//
//    
//
//
//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}
//
///*
//#pragma mark - Navigation
//
//// In a storyboard-based application, you will often want to do a little preparation before navigation
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//    // Get the new view controller using [segue destinationViewController].
//    // Pass the selected object to the new view controller.
//}
//*/

@end
